Skip to content

Conversation

@FriedJannik
Copy link
Member

Pull Request

Description of Changes

This PR fixes an issue with MongoDB backend where adding or updating SubmodelElements within an Entity element fails. The root cause was that MongoFilterBuilder.parse() always used "value" as the key for nested elements, but Entity stores its children in "statements" according to the AAS metamodel.

Changes made:

  1. MongoFilterBuilder.java:

    • Modified parse() method to accept an optional list of parent SubmodelElement objects
    • Added logic to check if a parent element is an Entity and use "statements" instead of "value" for the MongoDB update path
  2. MongoDbSubmodelOperations.java:

    • Added getParentElements() helper method that traverses the idShortPath and collects all parent SubmodelElements
    • Updated updateSubmodelElement() to call getParentElements() before building the MongoDB filter, providing type information to correctly determine the path

Related Issue

Fixes issue where MongoDB write operations fail with error:

Write error: WriteError{code=2, message='Plan executor error during update :: caused by :: The path 'submodelElements.0.value' must exist in the document in order to apply array updates.', details={}}

BaSyx Configuration for Testing

Standard MongoDB backend configuration for Submodel Service:

basyx:
  backend: MongoDB
  mongodb:
    host: localhost
    port: 27017
    database: basyx

AAS Files Used for Testing

Any Submodel containing nested Entity elements can be used for testing. Example:

{
  "idShort": "TestSubmodel",
  "id": "http://example.org/submodel",
  "submodelElements": [
    {
      "modelType": "Entity",
      "idShort": "OuterEntity",
      "entityType": "CoManagedEntity",
      "statements": [
        {
          "modelType": "Entity",
          "idShort": "InnerEntity",
          "entityType": "CoManagedEntity",
          "statements": []
        }
      ]
    }
  ]
}

Test scenario:

  1. Create a Submodel with an Entity element
  2. POST a new SubmodelElement to the Entity via /submodels/{submodelId}/submodel-elements/{entityIdShort}
  3. Verify the element is added to the Entity's statements

Additional Information

  • The fix maintains backward compatibility - existing functionality for SubmodelElementCollection and SubmodelElementList remains unchanged
  • The getParentElements() method performs multiple database queries to traverse the path, which could be optimized in the future if performance becomes a concern
  • Read operations (buildAggregationOperations) already handled both value and statements correctly using OR conditions, so no changes were needed there

@FriedJannik
Copy link
Member Author

closes #967

@aaronzi aaronzi merged commit 6770f59 into eclipse-basyx:main Jan 28, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants